package com.pac.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.pac.MyApp;
import com.pac.R;


/**
 * 加载图片的工具
 * Created by 兴盛 on 2015/4/22.
 */
public class ImageUtils {
    public static boolean mLoadOnlyWifi;

    /**
     * 使用XUtils的BitmapUtils加载图片，加载失败时自动处理。全部使用setBitmap设置图片，兼容CircleImageView
     *
     * @param context   上下文，用于判断网络状态
     * @param imageView 要加载图片的容器
     * @param uri       图片路径
     */
    public static void loadImg(final Context context, ImageView imageView, String uri) {
        loadImg(context, imageView, uri, false);
    }

    /**
     * 使用XUtils的BitmapUtils加载图片，加载失败时自动处理。全部使用setBitmap设置图片，兼容CircleImageView
     *
     * @param context   上下文，用于判断网络状态
     * @param imageView 要加载图片的容器
     * @param uri       图片路径
     * @param circle    是否加载为圆形图片
     */
    public static void loadImg(final Context context, ImageView imageView, String uri, final boolean circle) {
        if (!NetUtils.isNetOk(context)) {
            if (circle) {
                imageView.setImageDrawable(new CircleImageDrawable(BitmapFactory.decodeResource(context.getResources(), R.drawable.img_failed)));
            } else {
                imageView.setImageResource(R.drawable.img_failed);
            }
            return;
        }
        if (mLoadOnlyWifi && !NetUtils.isWifi(context)) {
            if (circle) {
                imageView.setImageDrawable(new CircleImageDrawable(BitmapFactory.decodeResource(context.getResources(), R.drawable.img_onlywifi)));
            } else {
                imageView.setImageResource(R.drawable.img_onlywifi);
            }
            return;
        }
//        BitmapDisplayConfig config = new BitmapDisplayConfig();
//        config.setLoadingDrawable(context.getResources().getDrawable(R.drawable.img_default));
        MyApp.bitmapUtils.display(imageView, uri
//                , config
                , new BitmapLoadCallBack<ImageView>() {
                    @Override
                    public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
                        if (circle) {
                            imageView.setImageDrawable(new CircleImageDrawable(bitmap));
                        } else {
                            imageView.setImageBitmap(bitmap);
                        }
                    }

                    @Override
                    public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {
                        if (circle) {
                            imageView.setImageDrawable(new CircleImageDrawable(BitmapFactory.decodeResource(context.getResources(), R.drawable.img_failed)));
                        } else {
                            imageView.setImageResource(R.drawable.img_failed);
                        }
                    }
                }
        );


    }
}
